package team.tunan.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.tunan.common.HttpCodeEnum;
import team.tunan.dto.StockoutDto;
import team.tunan.dto.StockoutPageDto;
import team.tunan.entity.Stockout;
import team.tunan.exception.BusinessException;
import team.tunan.mapper.StockoutMapper;
import team.tunan.service.IClientService;
import team.tunan.service.IEmployeeService;
import team.tunan.service.IStockService;
import team.tunan.service.IStockoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售表	 服务实现类
 * </p>
 *
 * @author sqx
 * @since 2023-12-24
 */
@Service
public class StockoutServiceImpl extends ServiceImpl<StockoutMapper, Stockout> implements IStockoutService {

    @Autowired
    private IStockService iStockService;

    @Autowired
    private StockoutMapper stockoutMapper;
    @Autowired
    private IClientService iClientService;

    @Autowired
    private IEmployeeService iEmployeeService;

    @Override
    @Transactional
    public int insertStockout(Stockout stockout) {
        if(stockout.getNumber() <= 0) {
            throw new BusinessException(HttpCodeEnum.OPERATION_ERROR.getCode(), "销售量必须大于0");
        }
        stockoutMapper.insert(stockout);
        iStockService.reduceStockNumber(stockout.getStockId(),stockout.getNumber());
        return 1;
    }

    @Override
    public Page<StockoutDto> selectPage(StockoutPageDto stockoutPageDto) {
        Page<Stockout> page = new Page<>(stockoutPageDto.getCurrentPage(),stockoutPageDto.getPageSize());
        LambdaQueryWrapper<Stockout> queryWrapper = new LambdaQueryWrapper<>();
        if(stockoutPageDto.getClientId()!=null){
            queryWrapper.like(Stockout::getClientId,stockoutPageDto.getClientId());
        }
        if(stockoutPageDto.getStockId()!=null){
            queryWrapper.eq(Stockout::getStockId,stockoutPageDto.getStockId());
        }
        if(stockoutPageDto.getRole() == 3){
            queryWrapper.eq(Stockout::getEmployeeId,stockoutPageDto.getEmployeeId());
        }
        Page<Stockout> stockoutPage = stockoutMapper.selectPage(page, queryWrapper);

        List<Stockout> records = stockoutPage.getRecords();
        List<StockoutDto> collect = records.stream().map(item -> {
            StockoutDto stockoutDto = new StockoutDto();
            BeanUtils.copyProperties(item, stockoutDto);
            stockoutDto.setClientName(iClientService.getById(item.getClientId()).getName());
            stockoutDto.setEmployeeName(iEmployeeService.getById(item.getEmployeeId()).getUserName());
            stockoutDto.setStockName(iStockService.getById(item.getStockId()).getStockName());
            return stockoutDto;
        }).collect(Collectors.toList());
        Page<StockoutDto> stockoutDtoPage = new Page<>();
        stockoutDtoPage.setRecords(collect);
        stockoutDtoPage.setSize(stockoutPage.getSize());
        stockoutDtoPage.setTotal(stockoutPage.getTotal());
        return stockoutDtoPage;
    }
}
